package cn.hzc.fortess.system.controller;

import cn.hzc.fortess.core.common.BizErrorEnum;
import cn.hzc.fortess.core.common.ResponseData;
import cn.hzc.fortess.core.common.constant.Const;
import cn.hzc.fortess.core.common.exception.ServiceException;
import cn.hzc.fortess.system.entity.BizLog;
import cn.hzc.fortess.system.search.BizLogSearch;
import cn.hzc.fortess.system.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags={"业务日志管理"})
@Controller
@RequestMapping("/sam/bizlog")
public class BizLogController extends BaseController {

    @Autowired
    private LogService logService;

    @RequestMapping(value = "",method = RequestMethod.GET)
    public String index() {
        return buildToView("index.html");
    }

    @ApiOperation(value = "获取分页列表")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Object page(BizLogSearch search) {
        return this.logService.getBizLogPage(search);
    }


    @ApiOperation(value = "获取日志详情")
    @RequestMapping(value = "/detail/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Object detail(@PathVariable Long id) {
        BizLog bizLog = logService.getBizLogById(id);
        final String message = bizLog.getLogDetail();
        if (StringUtils.isNotBlank(message) && message.length() >= 100) {
            String subMessage = message.substring(0, 100) + "...";
            bizLog.setLogDetail(subMessage);
        }

        if (StringUtils.isNotBlank(message) && message.contains(Const.LOG_SPLIT)) {
            String[] msgs = message.split(Const.LOG_SPLIT);
            bizLog.setRegularMessage(msgs);
        } else {
            bizLog.setRegularMessage(new String[]{message});
        }

        return bizLog;
    }
    @ApiOperation(value = "批量删除日志")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Object delete(String ids) {
        if(StringUtils.isBlank(ids)){
            throw new ServiceException(BizErrorEnum.REQUEST_NULL);
        }

        logService.deleteBizLog(ids.split(","));

        return ResponseData.success();
    }

    @Override
    protected String moduleName() {
        return "bizlog";
    }
}
